package com.hs.mobile.dao.category;


import java.util.List;

import com.hs.mobile.model.Category;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class CategoryDAOImpl implements CategoryDAO {
	
	/**
	 * 
	 * SessionFactory
	 */
	SessionFactory sessionFactory;
	
	//setter SessionFactory
	public void setSessionFactory( SessionFactory  sessionFactory )
	{
		this.sessionFactory = sessionFactory;
	}
	
	//getter SessionFactory
	public SessionFactory getSessionFactory()
	{
		return sessionFactory;
	}

	@Override
	public List getListAll() {
		// TODO Auto-generated method stub
		System.out.println("__ Get list categories __ ");
		Session session = getSessionFactory().openSession();
		List<Category> category = null;
		try {			
			Criteria cr = session.createCriteria(Category.class);
			category = cr.list();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Out __ ");
		} finally {
			session.close();
		}	
		return category;
	}

	@Override
	public Object getById(long id) {
		// TODO Auto-generated method stub
		System.out.println("__ Get category __ ");
		Session session = getSessionFactory().openSession();
		Category category = null;
		try {
			category = (Category) session.get( Category.class, id);
			System.out.println("__ get successfully __ ");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __ ");
		} finally {
			session.close();
		}	
		return category;
	}

	@Override
	public void add(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Save category __ ");
		Category category = (Category ) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.save(category);
			session.getTransaction().commit();
			System.out.println("__ save successfully __ ");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __ ");
		} finally {
			session.close();
		}					
	}

	@Override
	public void delete(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Delete category __ ");
		Category category = (Category ) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(category);
			session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Can not delete __ ");
		} finally {
			session.close();
		}	
	}

	@Override
	public void update(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Update category __ ");
		Category category = (Category ) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.update(category);
			session.getTransaction().commit();
			System.out.println("__ Update successfully __ ");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Out __ ");
		} finally {
			session.close();
		}	
	}

	@Override
	public Category findByCategoryId(long id) {
		// TODO Auto-generated method stub
		System.out.println("__ find category __ ");
		Session session = getSessionFactory().openSession();
		Category category = null;
		try {
			System.out.println("Id =  " + id );
			category = (Category) session.get( Category.class, id);
			System.out.println("__ find successfully __ ");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Can't get category __ ");
		} finally {
			session.close();
		}	
		return category;
	}

}
